package com.linovi.stringbuilder

class StringBuilder {
  var list: LinkedList = new LinkedList
  var length: Int = 0

  def append(x: String): StringBuilder = {

    var node = new Node
    node.element = x.toCharArray()
    length += node.element.length
    list.add(node)

    return this
  }

  def append(x: Int): StringBuilder =
    append(String.valueOf(x))

  def append(x: Boolean): StringBuilder =
    append(x.toString)

  def append(x: Double): StringBuilder =
    append(x.toString)

  def append(x: Float): StringBuilder =
    append(x.toString)

  def append(x: Byte): StringBuilder =
    append(x.toString)

  def append(x: Short): StringBuilder =
    append(x.toString)

  def append(x: Long): StringBuilder =
    append(x.toString)

  def append(x: Char): StringBuilder =
    append(x.toString)

  def result: Array[Char] =
    {
      var res: Array[Char] = new Array[Char](length)
      var n = list.head
      var last = 0

      while (n != null) {
        /*
        for (i <- 0 until n.element.length) {
          res(last) = n.element(i)
          last += 1
        }*/
        Array.copy(n.element, 0, res, last, n.element.length)
        n = n.next
      }
      res
    }
  override def toString() = new String(result)
}